package com.tina.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tina.entity.TiOnline;
import com.tina.entity.TiRole;
import com.tina.mapper.TiOnlineMapper;
import com.tina.model.ResultFormat;
import com.tina.model.ResultFormatPaging;
import com.tina.service.TiOnlineService;
import com.tina.service.impl.TiOnlineServiceImpl;
import com.tina.util.ResultPagingUtil;
import com.tina.util.ResultUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 在线用户 前端控制器
 * </p>
 *
 * @author Tina
 * @since 2020-06-08
 */
@RestController
@RequestMapping("/tiOnline/")
public class TiOnlineController {

    @Autowired
    private TiOnlineService onlineService;
    @Autowired
    private TiOnlineMapper onlineMapper;

    private Logger logger = LoggerFactory.getLogger(TiOnlineServiceImpl.class);

    /**
     * 查看所有在线用户
     *
     * @return
     */
    @GetMapping("queryOnlineUser")
    public ResultFormatPaging queryOnlineUser(TiOnline online, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        logger.info("online={}", online);
        IPage<TiOnline> onlineIPage = onlineService.queryOnlineUser(online, page, limit);
        List<TiOnline> records = onlineIPage.getRecords();
        return ResultPagingUtil.pagingSuccess(0, onlineIPage.getTotal(), records);
    }

    /**
     * 强制注销当前登录用户
     *
     * @param onlineSessionId
     * @return
     */
    @PutMapping("forceLogout")
    public ResultFormat forceLogout(String onlineSessionId) {
        logger.info("onlineSessionId={}", onlineSessionId);
        if (onlineSessionId.equalsIgnoreCase((String) SecurityUtils.getSubject().getSession().getId())) {
            return ResultUtil.error(100, "无法强退当前登陆用户");
        }
        TiOnline readSession = onlineMapper.selectById(onlineSessionId);
        if (readSession != null) {
            if (readSession.getOnlineState().equals(1)) {
                return ResultUtil.error(100, "用户已下线");
            }
        }
        try {
            onlineService.forceLogout(onlineSessionId);
            return ResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("e.getMessage()={}", e.getMessage());
            return ResultUtil.error(100, "踢出用户失败");
        }
    }

    /**
     * 批量强制注销当前登录用户
     *
     * @param onlineSessionId
     * @return
     */
    @PutMapping("forceLogouts")
    public ResultFormat forceLogouts(@RequestParam(value = "onlineSessionId[]") String[] onlineSessionId) {
        for (String sessionId : onlineSessionId) {
            logger.info("onlineSessionId={}", sessionId);
            if (sessionId.equalsIgnoreCase((String) SecurityUtils.getSubject().getSession().getId())) {
                return ResultUtil.error(100, "无法强退当前登陆用户");
            }
            TiOnline readSession = onlineMapper.selectById(sessionId);
            if (readSession != null) {
                if (readSession.getOnlineState().equals(1)) {
                    return ResultUtil.error(100, "用户已下线");
                }
            }
            onlineService.forceLogout(sessionId);
        }
        return ResultUtil.success();

    }

}
